package com.学生案例2;

import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/add")
public class addDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        Student student = new Student();
        try {
            BeanUtils.populate(student, parameterMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("/Users/eddie/workspace/java/work/My_servlet1/src/com/学生案例2/a.txt",true));
        bufferedWriter.write(student.getName() + " " + student.getAge() + " " + student.getResult());
        bufferedWriter.newLine();
        bufferedWriter.close();

        resp.setHeader("Refresh", "2;URL=/stu/html/index.html");
        resp.setContentType("text/html;Charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.write("添加成功，2秒后自动跳转首页..");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
